﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using System.Data.SqlClient;
using Capstone_project.DatabaseAccessLayer;
using Capstone_project.BussnessLogicLayer;

namespace Capstone_project
{
    public partial class frmEditTask : Form
    {
        SqlConnection con=Common.con;
        SqlDataAdapter adap;
        DataTable dt=new DataTable();

        TaskBUS taskbus = new TaskBUS();
        ProjectBUS projectbus = new ProjectBUS();
        TaskGroupBUS taskgroupbus = new TaskGroupBUS();
        PriorityBUS prioritybus = new PriorityBUS();
        string project123;
        public frmEditTask()
        {
            InitializeComponent();
        }

        private void btnUpdate_Click(object sender, EventArgs e)
        {
            try
            {
                if (txtIDTask.Text == "" || txtNameTask.Text == ""|| cbxTaskGroup.Text=="" || cbxPriority.Text=="" || txtTimes.Text=="")
                {
                    MessageBox.Show("You must enter all infomation", "", MessageBoxButtons.OK, MessageBoxIcon.Warning);
                }
                else
                {
                    string sql = "UPDATE TASK SET NAME_TASK='" + txtNameTask.Text + "',ID_TASK_GROUP='" + cbxTaskGroup.SelectedValue + "',TIMES=" + txtTimes.Text + ",ID_PRIORITY='" + cbxPriority.SelectedValue + "' WHERE ID_TASK='" + txtIDTask.Text + "'";
                    if (con.State != ConnectionState.Open)
                        con.Open();
                    SqlCommand com = new SqlCommand(sql, con);
                    adap = new SqlDataAdapter(com);
                    adap.Fill(dt);
                    con.Close();
                    MessageBox.Show("Task update successfully !");
                    this.Close();
                }
            }
            catch { MessageBox.Show("Can't edit this task! Please check again relationship."); }
        }

        private void frmEditTask_Load(object sender, EventArgs e)
        {
            string IDTASK = Common.selectedIDTASK;
            if (IDTASK == "")
            {
                MessageBox.Show("Please select a task to edit");
            }
            else
            {
                txtIDTask.Text = IDTASK;
                DataTable dt1 = new DataTable();
                dt1 = projectbus.getList();
                cbxProject.DataSource = dt1;
                cbxProject.DisplayMember = "NAME_PROJECT";
                cbxProject.ValueMember = "ID_PROJECT";

                cbxPriority.DataSource = prioritybus.getList();
                cbxPriority.DisplayMember = "NAME_PRIORITY";
                cbxPriority.ValueMember = "ID_PRIORITY";

                string sql = "SELECT TASK.ID_TASK, TASK.NAME_TASK, PROJECT.NAME_PROJECT, TASK_GROUP.NAME_TASK_GROUP, TASK.TIMES, PRIORITY.NAME_PRIORITY FROM PROJECT INNER JOIN (PRIORITY INNER JOIN (TASK_GROUP INNER JOIN (ASSIGN_TASK_STATUS INNER JOIN TASK ON ASSIGN_TASK_STATUS.ID_TASK_STATUS = TASK.ID_TASK_STATUS) ON TASK_GROUP.ID_TASK_GROUP = TASK.ID_TASK_GROUP) ON PRIORITY.ID_PRIORITY = TASK.ID_PRIORITY) ON PROJECT.ID_PROJECT = TASK_GROUP.ID_PROJECT WHERE ID_TASK='" + IDTASK + "'";
                if (con.State != ConnectionState.Open)
                    con.Open();
                SqlCommand com = new SqlCommand(sql, con);
                adap = new SqlDataAdapter(com);
                DataTable dtnew = new DataTable();
                adap.Fill(dtnew);
                con.Close();
                txtNameTask.Text = dtnew.Rows[0][1].ToString();
                cbxProject.Text = dtnew.Rows[0][2].ToString();
                cbxTaskGroup.Text = dtnew.Rows[0][3].ToString();
                txtTimes.Text = dtnew.Rows[0][4].ToString();
                cbxPriority.Text = dtnew.Rows[0][5].ToString();

            }
        }

        private void cbxProject_SelectedIndexChanged(object sender, EventArgs e)
        {
            project123 = cbxProject.SelectedValue.ToString();

            cbxTaskGroup.DataSource = taskgroupbus.getTaskGrBYPROJECT(project123);
            cbxTaskGroup.DisplayMember = "NAME_TASK_GROUP";
            cbxTaskGroup.ValueMember = "ID_TASK_GROUP";
        }

        private void btnCancel_Click(object sender, EventArgs e)
        {
            this.Close();
        }

        #region Chặn ko cho nhập chữ. Chỉ cho nhập số và dấu '.'
        private void txtTimes_KeyPress(object sender, KeyPressEventArgs e)
        {
            if (!char.IsControl(e.KeyChar) && !char.IsDigit(e.KeyChar) && (e.KeyChar != '.' ||
                (e.KeyChar == '.' && (txtTimes.Text.Length == 0 || txtTimes.Text.IndexOf('.') != -1))))
                e.Handled = true;
        }
        #endregion
    }
}
